﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Model.Domain;
using System.Data.Common;
using IMPlant.Core.Data.Unity;
using System.Data;
using SMTC.Model.Model;

namespace SMTC.Dao.BU
{
    public class BUQueryDao : BaseDao
    {
        /// <summary>
        /// 获取bu列表
        /// </summary>
        private const string SQL_SELECT_BU = @"SELECT BUID,
	                                                   BUName,
	                                                   PlantID,
	                                                   BUCapacity,
	                                                   ModifyPerson,
	                                                   ModifyDateTime
                                                FROM dbo.SMT_BU
                                                WHERE BUName LIKE @BUName
                                                AND ModifyPerson LIKE @ModifyPerson
                                                AND ModifyDateTime BETWEEN @startTime AND @endTime";
        /// <summary>
        /// 获取bu下的生产线列表
        /// </summary>
        private const string SQL_SELECT_BU_LINE = @"SELECT LineID,
	                                                       LineName,
	                                                       LineTargetTime,
	                                                       BUID,
	                                                       ModifyPerson,
	                                                       ModifyDateTime,
	                                                       LineCapacity FROM dbo.SMT_Line
                                                    WHERE BUID = @BUID";
        /// <summary>
        /// 获取单个bu实体
        /// </summary>
        private const string SQL_SELECT_SINGLE = @"SELECT BUID,
	                                                       BUName,
	                                                       PlantID,
	                                                       BUCapacity,
	                                                       ModifyPerson,
	                                                       ModifyDateTime FROM dbo.SMT_BU
                                                    WHERE BUID = @BUID";
        /// <summary>
        /// 获取bu列表
        /// </summary>
        /// <param name="BUName"></param>
        /// <param name="ModifyPerson"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<SMT_BUModel> GetBUList(string BUName, string ModifyPerson, DateTime startTime, DateTime endTime)
        {
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@BUName",BUName)
               ,DBparameterBuild.Build("@ModifyPerson",ModifyPerson)
               ,DBparameterBuild.Build("@startTime",startTime)
               ,DBparameterBuild.Build("@endTime",endTime)
            };

            return DBTemplete.ExcuteGetList<SMT_BUModel>(CommandType.Text, SQL_SELECT_BU, parameters);
        }

        /// <summary>
        /// 获取bu列表供选择BU时使用
        /// </summary>
        /// <param name="BUName"></param>
        /// <param name="ModifyPerson"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<SMT_BUModel> GetBUListForSelect(string BUName, string ModifyPerson, DateTime startTime, DateTime endTime)
        {
            string str = SQL_SELECT_BU + " and PlantID is null";
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@BUName",BUName)
               ,DBparameterBuild.Build("@ModifyPerson",ModifyPerson)
               ,DBparameterBuild.Build("@startTime",startTime)
               ,DBparameterBuild.Build("@endTime",endTime)
            };

            return DBTemplete.ExcuteGetList<SMT_BUModel>(CommandType.Text, str, parameters);
        }

        /// <summary>
        /// 获取单个bu实体
        /// </summary>
        /// <param name="BUID"></param>
        /// <returns></returns>
        public SMT_BUModel GetBU(string BUID)
        {
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@BUID",BUID)
            };

            return DBTemplete.ExcuteGetObject<SMT_BUModel>(CommandType.Text, SQL_SELECT_SINGLE, parameters);
        }
        /// <summary>
        /// 获取bu下的生产线list
        /// </summary>
        /// <param name="BUID"></param>
        /// <returns></returns>
        public List<SMT_LineModel> GetLineList(string BUID)
        {
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@BUID",BUID)
            };

            return DBTemplete.ExcuteGetList<SMT_LineModel>(CommandType.Text, SQL_SELECT_BU_LINE, parameters);
        }
    }
}
